package pacificlms.Verifiable.Database;

import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import pacificlms.Email.EmailManager;
import pacificlms.Error.ErrorHandle;
import pacificlms.Verifiable.*;

/**
 * Hold is a child of VerifiableObject dealing with Holds
 * Since all fields are get/set they are not individually commented
 * @author Brian Hume
 */
public class Hold extends VerifiableObject
{
    @Override
    public String TableName() {
        return "Hold";
    }
    
    private String ResourceCode;
    @VerifyAnnotation(ApplicationName="Resource Code", DBName="ResourceCode",  Type=TypeEnum.LOOKUP, Length=50, PrimaryKey=true, LookupDBTable="Resource", LookupDBField="ResourceCode")
    public String ResourceCode(String value)
    {
        return ResourceCode = StandardProcess(ResourceCode, value);
    }
    
    
    private String IdentityNumber;
    @VerifyAnnotation(ApplicationName="Identity Number", DBName="IdentityNumber",  Type=TypeEnum.NUMERICSTRING, Length=8, ExactLength=true, PrimaryKey=true, LookupDBTable="Member", LookupDBField="IdentityNumber")
    public String IdentityNumber(String value)
    {
        return IdentityNumber = StandardProcess(IdentityNumber, value);
    }
    
    
    private String RequestDate;
    @VerifyAnnotation(ApplicationName="Request Date", DBName="RequestDate",  Type=TypeEnum.DATE, Length=50)
    public String RequestDate(String value)
    {
        return RequestDate = StandardProcess(RequestDate, value);
    }
    
    
    private String EndDate;
    @VerifyAnnotation(ApplicationName="End Date", DBName="EndDate",  Type=TypeEnum.DATE, Length=50, NotNull=false)
    public String EndDate(String value)
    {
        return EndDate = StandardProcess(EndDate, value);
    }
    
    private String Finished;
    @VerifyAnnotation(ApplicationName="Finished", DBName="Finished",  Type=TypeEnum.STRING, Length=5)
    public String Finished(String value)
    {
        return Finished = StandardProcess(Finished, value);
    }
    public boolean GetFinishedBoolean()
    {
        if (Finished.equalsIgnoreCase("TRUE") || Finished.equalsIgnoreCase("1"))
        {
            return true;
        }
        if (Finished.equalsIgnoreCase("FALSE") || Finished.equalsIgnoreCase("0"))
        {
            return false;
        }
        return false;
    }
    
    @Override
    protected boolean ExtraVerification()
    {
        if (Finished.equalsIgnoreCase("TRUE") || Finished.equalsIgnoreCase("FALSE") ||
            Finished.equalsIgnoreCase("1") || Finished.equalsIgnoreCase("0"))
        {
            if (Finished.equalsIgnoreCase("TRUE")) Finished = "1";
            if (Finished.equalsIgnoreCase("FALSE")) Finished = "0";
            return true;
        }
        
        ErrorHandle.Display("Input Error", "Finished must be either true or false", null, true);
        return false;
    }
    
    public static boolean EmailNextHolder(String ResourceCode, Statement stmt)
    {
        try
        {
            GregorianCalendar now= new GregorianCalendar();
            SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
            String CurrentDate=sf.format(now.getTime());
            
            Hold HD = new Hold();
            Hold HoldEmail = new Hold();
            HD.ResourceCode(ResourceCode);
            HD.Finished("0");
            String SQL = HD.FindIdenticalSQL();
            ResultSet HoldRS = stmt.executeQuery(SQL);
            while(HoldRS.next())
            {
                HD = (Hold)Hold.CreateFromResultSet(Hold.class, HoldRS, HoldRS.getMetaData());
                if(sf.parse(HD.RequestDate(null)).before(sf.parse(HoldEmail.RequestDate(null))))
                    HoldEmail = (Hold)Hold.CreateFromResultSet(Hold.class, HoldRS, HoldRS.getMetaData());
            }
            if(!HoldEmail.RequestDate(null).equals("2050-01-01"))
            {
                Member Memb = new Member();
                Memb.IdentityNumber(HoldEmail.IdentityNumber(null));
                SQL=Memb.FindIdenticalSQL();
                HoldRS = stmt.executeQuery(SQL);
                HoldRS.next();
                Memb = (Member)Member.CreateFromResultSet(Member.class, HoldRS, HoldRS.getMetaData());
                
                HoldEmail.EndDate(CurrentDate);
                SQL = HoldEmail.UpdateRecrodSQL();
                stmt.executeUpdate(SQL);
                EmailManager.HoldReadyEmail(Memb, HoldEmail);
                
                return true;
            }
        }
        catch(Exception Ex)
        {
            
        }
        return false;
    }
    
    @Override
    public String toString()
    {
        String Str = "[" + ResourceCode +"] Held for member #" + IdentityNumber + " on " + RequestDate;
        if (0==0)//fixme if due in future
        {
            Str = Str + " and must be checked out by " + EndDate;
        }
        return Str;
    }
}
